/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "events_init.h"
#include <stdio.h>
#include "lvgl.h"
#include "light.h"

#if LV_USE_GUIDER_SIMULATOR && LV_USE_FREEMASTER
#include "freemaster_client.h"
#endif


static void Home_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_SCREEN_LOADED:
    {
        break;
    }
    case LV_EVENT_SCREEN_UNLOADED:
    {
        break;
    }
    default:
        break;
    }
}

static void Home_led_slider_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_VALUE_CHANGED:
    {
        lv_obj_t* led_slider = lv_event_get_target_obj(e);
        int32_t value = lv_slider_get_value(led_slider);
        light_led_on(value&0xFF);
        break;
    }
    default:
        break;
    }
}

static void Home_rgb_btn_1_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    static bool light_flag = false;
    switch (code) {
    case LV_EVENT_CLICKED:
    {
         uint8_t brightness = light_ws2812_get_brightness(0);
        if(light_flag)
        {
            light_ws2812_on(0,0,0,0);
            light_flag = false;
        }
        else
        {
            if(!brightness)
                light_ws2812_on(0,20,20,20);
            light_flag = true;
        }
        break;
    }
    default:
        break;
    }
}

static void Home_rgb_btn_3_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    static bool light_flag = false;
    switch (code) {
    case LV_EVENT_CLICKED:
    {
         uint8_t brightness = light_ws2812_get_brightness(2);
        if(light_flag)
        {
            light_ws2812_on(2,0,0,0);
            light_flag = false;
        }
        else
        {
            if(!brightness)
                light_ws2812_on(2,20,20,20);
            light_flag = true;
        }
        break;
    }
    default:
        break;
    }
}

static void Home_rgb_btn_2_event_handler (lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    static bool light_flag = false;
    switch (code) {
    case LV_EVENT_CLICKED:
    {
        uint8_t brightness = light_ws2812_get_brightness(1);
        if(light_flag)
        {
            light_ws2812_on(1,0,0,0);
            light_flag = false;
        }
        else
        {
            if(!brightness)
                light_ws2812_on(1,20,20,20);
            light_flag = true;
        }
        break;
    }
    default:
        break;
    }
}

void events_init_Home (lv_ui *ui)
{
    lv_obj_add_event_cb(ui->Home, Home_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->Home_led_slider, Home_led_slider_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->Home_rgb_btn_1, Home_rgb_btn_1_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->Home_rgb_btn_3, Home_rgb_btn_3_event_handler, LV_EVENT_ALL, ui);
    lv_obj_add_event_cb(ui->Home_rgb_btn_2, Home_rgb_btn_2_event_handler, LV_EVENT_ALL, ui);
}


void events_init(lv_ui *ui)
{

}
